package com.giscloud.amp.controller.sys;

import com.giscloud.amp.controller.BaseController;
import com.giscloud.amp.entity.AmpUpdateLog;
import com.giscloud.amp.entity.dto.UpdateLogDTO;
import com.giscloud.amp.service.sys.UpdateLogService;
import com.giscloud.commons.model.Result;
import com.giscloud.datasource.page.PageInfo;
import com.giscloud.token.utils.Token;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author gxd
 * @data 2018/7/14 10:59
 */
@SuppressWarnings("SpringJavaAutowiringInspection") //加这个注解让IDE 不报: Could not autowire
@RestController
public class UpdateLogController extends BaseController {

    @Autowired
    UpdateLogService updateLogRemoteClient;

    private static final Logger log = LoggerFactory
            .getLogger(UpdateLogController.class);

    @RequestMapping(value="/amp/updateLog/list",method = RequestMethod.POST)

    public PageInfo getList(@RequestBody UpdateLogDTO updateLogDTO) {

        return updateLogRemoteClient.getList(updateLogDTO);
    }

    @RequestMapping(value="/amp/updateLog/add", method = RequestMethod.POST)
    public Result add(@RequestBody AmpUpdateLog ampUpdateLog){
        log.error("===========添加方法===========");
        Result result = new Result();
        try {
            ampUpdateLog.setCreatedDt(new Date());
            Token ssoToken = getToken();
            if(ssoToken == null){
                ssoToken.setStaffId(421L);
            }
            ampUpdateLog.setCreatedStaff(ssoToken.getStaffId());
            ampUpdateLog.setStatusCd(1);
            ampUpdateLog.setVersion(ampUpdateLog.getCreatedDt());
            ampUpdateLog.setOperateStaff(ssoToken.getStaffId());
            result = updateLogRemoteClient.add(ampUpdateLog);
            return result;
        } catch (Exception e) {
            result.setMsg("添加失败");
            result.setSuccess(false);
            result.setCode(-1);
            return result;
        }
    }
    @RequestMapping(value="/amp/deleteUpdateLog", method = RequestMethod.POST)
    public Result delete(@RequestBody String logId){
        Result result = new Result();
        try {
            updateLogRemoteClient.delete(Long.valueOf(logId));
            result.setCode(0);
            result.setSuccess(true);
            result.setMsg("删除成功");
            return result;
        } catch (Exception e) {
            result.setCode(-1);
            result.setSuccess(false);
            result.setMsg("删除失败");
            return result;
        }
    }
}
